﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DTTB.Common;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.FormBean.DanhMuc;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.LinQ.DanhMuc;
using Webdiyer.WebControls.Mvc;

namespace DTTB.MV2.QUANLYNHANSUTIENLUONG.BussinessDelegate.DanhMucDelegate
{
    public class TonGiaoDelegate
    {
        DBDanhMucDataContext DBDanhMuc = new DBDanhMucDataContext();

        public IList<dm_TonGiao> GetAllTonGiao()
        {
            return DBDanhMuc.dm_TonGiaos.Where(p => p.IsDelete == false).ToList();
        }
        public dm_TonGiao AddNewTonGiao(dm_TonGiao TonGiao)
        {
            DBDanhMuc.dm_TonGiaos.InsertOnSubmit(TonGiao);

            DBDanhMuc.SubmitChanges();

            return GetLoaiCongChucIdMax();
        }

        public void DeleteTonGiao(int TonGiaoID)
        {
            dm_TonGiao TonGiaoNew =
                DBDanhMuc.dm_TonGiaos.Single(p => p.Id == TonGiaoID);
            //LoaiCongChuc.Id = LoaiCongChuc.Id;

            DBDanhMuc.dm_TonGiaos.DeleteOnSubmit(TonGiaoNew);
            DBDanhMuc.SubmitChanges();
        }
        public int DeleteTonGiao(dm_TonGiao TonGiao)
        {
            dm_TonGiao TonGiaoDelete =
                DBDanhMuc.dm_TonGiaos.Single(p => p.Id == TonGiao.Id);
            if (TonGiaoDelete.hs_NhanViens.Count() == 0)
            {
                TonGiaoDelete.Id = TonGiao.Id;
                TonGiaoDelete.UpdateDate = DateTime.Now;
                TonGiaoDelete.UpdateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
                TonGiaoDelete.IsDelete = true;
                DBDanhMuc.SubmitChanges();
                return 1;
            }
            else
            {
                return 0;
            }
        }
        public void UpdateTonGiao(dm_TonGiao TonGiao)
        {
            dm_TonGiao TonGiaoUpdate =
                DBDanhMuc.dm_TonGiaos.Single(p => p.Id == TonGiao.Id);

            TonGiaoUpdate.Id = TonGiao.Id;
            TonGiaoUpdate.TenTonGiao = TonGiao.TenTonGiao;
            TonGiaoUpdate.UpdateDate = TonGiaoUpdate.UpdateDate;
            TonGiaoUpdate.UpdateBy = TonGiaoUpdate.UpdateBy;
            DBDanhMuc.SubmitChanges();
        }

        public PagedList<dm_TonGiao> SearchTonGiaoPagedList(int? id, string searchCriteria)// id = page Index
        {
            return (PagedList<dm_TonGiao>)DBDanhMuc.dm_TonGiaos.Where(p => p.TenTonGiao.Contains(searchCriteria) && p.IsDelete == false).ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);


        }

        public dm_TonGiao GetTonGiaoById(string id)
        {
            try
            {
                dm_TonGiao item = this.DBDanhMuc.dm_TonGiaos.Single(p => p.Id == Convert.ToInt32(id));
                return item;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public dm_TonGiao GetTonGiaoByTenCheck(string ten)
        {

            IEnumerable<dm_TonGiao> list = this.DBDanhMuc.dm_TonGiaos.Where(p => p.TenTonGiao.Equals(ten) && p.IsDelete == false);
            return list.Count() != 0 ? list.ElementAt(0) : null;
        }
        public dm_TonGiao GetTonGiaoByTen(string ten)
        {

            IEnumerable<dm_TonGiao> list = this.DBDanhMuc.dm_TonGiaos.Where(p => p.TenTonGiao == ten);
            return list.Count() != 0 ? list.ElementAt(0) : null;
        }
        public PagedList<dm_TonGiao> RecycleGetTonGiaoPagedList(int? id)// id = page Index
        {
            return DBDanhMuc.dm_TonGiaos.Where(p => p.IsDelete == true).OrderByDescending(p => p.Id).ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);
        }
        public PagedList<dm_TonGiao> GetTonGiaoPagedList(int? id)// id = page Index
        {
            return DBDanhMuc.dm_TonGiaos.Where(p => p.IsDelete == false).OrderByDescending(p => p.Id).ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);
        }

        public dm_TonGiao BuildNewTonGiao(TonGiaoForm form)
        {
            dm_TonGiao newTonGiao = new dm_TonGiao();
            newTonGiao.TenTonGiao = form.TenTonGiao;
            newTonGiao.CreateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
            newTonGiao.CreateDate = DateTime.Now;
            newTonGiao.IsDelete = false;
            return newTonGiao;
        }
        public int PhucHoiTonGiao(int id)
        {
            dm_TonGiao phuchoi = new dm_TonGiao();
            phuchoi = this.GetTonGiaoById(Convert.ToString(id));
            dm_TonGiao item = new dm_TonGiao();
            item = this.GetTonGiaoByTenCheck(phuchoi.TenTonGiao);
            if (item != null)
                return 1;
            else
            {
                phuchoi.IsDelete = false;
                DBDanhMuc.SubmitChanges();
                return 2;
            }
        }
        public dm_TonGiao BuildEditTonGiao(TonGiaoForm form)
        {
            dm_TonGiao newTonGiao = new dm_TonGiao();
            newTonGiao.Id = form.id;
            newTonGiao.TenTonGiao = form.TenTonGiao;
            newTonGiao.UpdateDate = DateTime.Now;
            newTonGiao.UpdateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
            return newTonGiao;
        }
        public dm_TonGiao GetLoaiCongChucIdMax()
        {
            IList<dm_TonGiao> list = DBDanhMuc.dm_TonGiaos.ToList();
            return list.Count != 0 ? list.ElementAt(list.Count - 1) : null;
        }
        public TonGiaoForm ConvertToForm(dm_TonGiao obj)
        {
            TonGiaoForm form = new TonGiaoForm();
            form.id = obj.Id;
            form.TenTonGiao = obj.TenTonGiao;
            form.UpdateDate = obj.UpdateDate != null ? obj.UpdateDate.Value : DateTime.MinValue;
            form.CreateDate = obj.CreateDate != null ? obj.CreateDate.Value : DateTime.MinValue;
            form.UpdateBy = obj.UpdateBy != null ? obj.UpdateBy.Value : 0;
            form.CreateBy = obj.CreateBy != null ? obj.CreateBy.Value : 0;
            return form;
        }
    }
}